package com.google.gwt.sample.dynatablerf.client.presenters;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.sample.dynatablerf.client.events.CloseProjetoListEvent;
import com.google.gwt.sample.dynatablerf.client.events.GoToProjetoEvent;
import com.google.gwt.sample.dynatablerf.shared.GrifoRequestFactory;
import com.google.gwt.sample.dynatablerf.shared.GrifoRequestFactory.ProjetoRequest;
import com.google.gwt.sample.dynatablerf.shared.GrifoSharedConstants;
import com.google.gwt.sample.dynatablerf.shared.GrifoSharedConvertUtil;
import com.google.gwt.sample.dynatablerf.shared.ProjetoProxy;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle.MultiWordSuggestion;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.gwt.user.client.ui.SuggestOracle.Callback;
import com.google.gwt.user.client.ui.SuggestOracle.Request;
import com.google.gwt.user.client.ui.SuggestOracle.Response;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class ProjetoListPresenter extends
		SingleSelectionDataPresenter<ProjetoProxy, ProjetoRequest> {

	class ProjetoWordSuggestion extends MultiWordSuggestion {

		private ProjetoProxy projetoProxy;

		public ProjetoWordSuggestion(ProjetoProxy projetoProxy) {
			super("", projetoProxy.getNumero() + " - "
					+ projetoProxy.getDescricao());
			this.projetoProxy = projetoProxy;
		}

		public ProjetoProxy getProjetoProxy() {
			return projetoProxy;
		}

	}

	private SingleSelectionDataDisplay<ProjetoProxy> display;
	private int numProjetos;

	public ProjetoListPresenter(
			SingleSelectionDataDisplay<ProjetoProxy> projetoDisplay,
			GrifoRequestFactory requestFactory, EventBus eventBus) {
		super(eventBus, requestFactory);
		this.display = projetoDisplay;
	}

	@Override
	protected SingleSelectionDataDisplay<ProjetoProxy> getSingleSelectionDataDisplay() {
		return display;
	}

	@Override
	protected void onSingleSelection(ProjetoProxy selectedItem) {
		getEventBus().fireEvent(
				new GoToProjetoEvent(selectedItem, getTabLayoutPanel()));
	}

	@Override
	protected void fetch(final int start, int length) {
		getRequestContext().findAll(start, length).with("tipoInstalacao")
				.fire(new Receiver<List<ProjetoProxy>>() {

					@Override
					public void onSuccess(List<ProjetoProxy> response) {
						if (response.isEmpty()) {
							getAsyncDataProvider().updateRowCount(0, true);

						} else {
							getAsyncDataProvider().updateRowCount(numProjetos,
									true);

						}
						getAsyncDataProvider().updateRowData(start, response);
					}
				});

	}

	@Override
	protected void fireCloseWindowEvent() {
		getEventBus().fireEvent(new CloseProjetoListEvent());

	}

	@Override
	protected void fetch() {
		getRequestContext().countAll().fire(new Receiver<Long>() {

			@Override
			public void onSuccess(Long response) {
				numProjetos = GrifoSharedConvertUtil.converToInteger(response);

			}
		});

	}

	@Override
	protected void fetchSuggestions(final Request request,
			final Callback callback) {
		getRequestContext().findAll(request.getQuery(),
				GrifoSharedConstants.SUGGESTION_MAX_RESULT).fire(
				new Receiver<List<ProjetoProxy>>() {

					@Override
					public void onSuccess(List<ProjetoProxy> response) {

						List<Suggestion> suggestions = new ArrayList<SuggestOracle.Suggestion>();
						if (response != null) {
							for (ProjetoProxy projetoProxy : response) {
								suggestions.add(new ProjetoWordSuggestion(
										projetoProxy));
							}
						}
						callback.onSuggestionsReady(request, new Response(
								suggestions));
					}
				});

	}

	@Override
	protected void onFetchSelection(SelectionEvent<Suggestion> event) {
		ProjetoWordSuggestion projetoWordSuggestion = (ProjetoWordSuggestion) event
				.getSelectedItem();
		getEventBus().fireEvent(
				new GoToProjetoEvent(projetoWordSuggestion.getProjetoProxy(),
						getTabLayoutPanel()));

	}

	@Override
	protected ProjetoRequest getRequestContext() {
		return getRequestFactory().projetoRequest();
	}

}